<template>
  <div class="toollibray-container">
    <div class="page-section">
      <div class="page-title">工具库</div>
      <el-form :inline="true" :model="dataForm">
        <el-form-item label="工具名称">
          <el-input
            v-model="dataForm.key"
            placeholder="搜素内容"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList">搜索</el-button>
          <el-button type="primary" @click="addOrUpdateHandle(0)"
            >添加</el-button
          >
          <!-- <el-button  type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
        </el-form-item>
      </el-form>
    </div>

    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%"
      :header-cell-style="{ backgroundColor: '#f2f2f2' }"
    >
      <el-table-column
        prop="toolName"
        header-align="center"
        align="center"
        label="名称"
      >
      </el-table-column>
      <el-table-column
        prop="toolUrl"
        header-align="center"
        align="center"
        label="链接地址"
      >
      </el-table-column>
      <el-table-column
        prop="isShow"
        header-align="center"
        align="center"
        label="是否导航栏显示"
      >
        <template v-slot="{ row }">
          {{ row.isShow == 1 ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        label="更新时间"
      >
        <template v-slot="{ row }">
          {{ row.updateTime | knowledgeTimeFormat }}
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-link type="primary" @click="addOrUpdateHandle(1, scope.row)"
            >修改</el-link
          >
          <el-link type="danger" @click="deleteHandle(scope.row.id)"
            >删除</el-link
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, prev, pager, next"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./components/toollibrary-add-or-update";
import { queryTool, delTool } from "@/api/knowlegeBase";
export default {
  data() {
    return {
      dataForm: {
        key: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      addOrUpdateVisible: false,
    };
  },
  components: {
    AddOrUpdate,
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      const params = {
        pageSize: this.pageSize,
        pageNum: this.pageIndex,
        searchName: this.dataForm.key,
      };
      queryTool(params)
        .then((res) => {
          const {
            data: { records, total },
          } = res;
          this.dataList = records;
          this.totalPage = total;
        })
        .catch(() => {
          this.$message.error(`工具库查询异常`);
        });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getList();
    },

    // 新增 / 修改
    addOrUpdateHandle(type, data) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(type, data);
      });
    },
    // 删除
    deleteHandle(id) {
      this.$confirm(`确定删除?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        if (this.dataList.length == 1) {
          this.pageIndex = 1;
        }
        delTool([id])
          .then(() => {
            this.$message.success(`删除成功`);
            this.getList();
          })
          .catch(() => {
            this.$message.error(`工具库删除异常`);
          });
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.el-link {
  margin: 4px;
}
</style>